﻿using UnityEngine;

public class CameraClipMgr : Singleton<CameraClipMgr>
{
    private CameraClipPanel view;
    private TweenColor tweenColor;
    private uint delayID;

    public bool IsFinished { get; private set; }
    private CameraPathBezierAnimator curPath;

    public CameraClipMgr()
    {
        Transform trans = AllUpdate.GetChildByName("CameraClip");
        view = new CameraClipPanel(trans.gameObject);
        tweenColor = view.full_sp.GetComponent<TweenColor>();

        Utility.SetActive(view.Go, false);
        UIEventListener.Get(view.full_wgt.gameObject).onClick = OnFullClick;
    }

    private void Init()
    {
        tweenColor.enabled = false; IsFinished = true;
        tweenColor.AddOnFinished(OnFinished);
    }

    private void OnClipFinished()
    {
        Utility.SetActive(view.Go, false);
        curPath = null; IsFinished = true;
    }

    public void OnFullClick(GameObject go)
    {
        if (curPath == null) { return; }
        curPath.StopAllCoroutines(); curPath.Stop();
        curPath.AnimationFinished -= AnimationFinished;
        ZMgr.TimerMgr.ClearTimeOut(delayID);
        OnClipFinished();
    }

    private void OnFinished()
    {
        Utility.SetActive(tweenColor, false);
        if (curPath == null)
        {
            OnClipFinished();
        }
    }

    public void Open(CameraPathBezierAnimator nCurPath)
    {
        view.name_sp.spriteName = "";   //场景名称图片
        curPath = nCurPath; IsFinished = false;

        Utility.SetActive(view.down_sp, true);
        Utility.SetActive(view.up_sp, true);
        Utility.SetActive(view.Go, true);

        view.full_sp.width = view.down_sp.width = view.up_sp.width = view.full_wgt.width = GameTool.ScreenWidth;
        int screenHeight = view.full_wgt.height = view.full_sp.height = GameTool.ScreenHeight;

        Utility.SetActive(view.full_sp, true);
        Vector3 pos = view.up_sp.transform.localPosition;
        pos.y = screenHeight * 0.5f - view.up_sp.height * 0.5f;
        view.up_sp.transform.localPosition = pos;

        pos = view.down_sp.transform.localPosition;
        pos.y = -screenHeight * 0.5f + view.down_sp.height * 0.5f;
        view.down_sp.transform.localPosition = pos;

        curPath.AnimationFinished += AnimationFinished;
        curPath.Play();
    }

    private void AnimationFinished()
    {
        curPath.AnimationFinished -= AnimationFinished;
        CameraPathBezierAnimator next = curPath.nextAnimation;
        Object.DestroyObject(curPath.gameObject);

        if (next != null)
        {
            curPath.nextAnimation = null;
            curPath = next;
        }
        else
        {
            curPath = null;
        }
        PlayTween();
    }

    private void PlayTween()
    {
        tweenColor.ResetToBeginning(); tweenColor.enabled = true;
        Utility.SetActive(tweenColor, true);
        delayID = ZMgr.TimerMgr.SetTimeOut(PlayAnimation, tweenColor.duration * 0.5f);
    }

    private void PlayAnimation()
    {
        if (curPath != null)
        {
            curPath.AnimationFinished += AnimationFinished;
            curPath.Play();
        }
        else
        {
            Utility.SetActive(view.down_sp, false);
            Utility.SetActive(view.up_sp, false);
            OnClipFinished();
        }
    }
}